package com.rudolfheszele.smsselector.model;

public class RuleFactory
{	
	public enum RuleId implements Id
	{
		IS_MESSAGE_FROM_ADDRESS("Is message from address", 0),
		IS_MESSAGE_CONTAINS("Is message contains", 1),
		IS_MESSAGE_OLDER("Is message older", 2),
		IS_MESSAGE_FROM_PROVIDER("Is message from provider", 3),
		IS_MESSAGE_FROM_PERSON("Is message from person", 4);
		
		@Override
		public int getIntValue()
		{
			return mIntValue;
		}
		
		@Override
		public String getName()
		{
			return mRuleName;
		}
		
		private RuleId(String ruleName, int intValue)
		{
			mRuleName = ruleName;
			mIntValue = intValue;
		}
		
		private int mIntValue;
		private String mRuleName;
	}
	
	public static SmsSelectorRuleSet.Rule createRule(RuleId ruleId, String additionalInfo) throws SmsSelectorRuleSetException 
	{
		SmsSelectorRuleSet.Rule rule = null;
		
		switch(ruleId)
		{
			case IS_MESSAGE_FROM_ADDRESS:
				rule = new IsMessageFromAddressRule(additionalInfo);
				break;
			case IS_MESSAGE_FROM_PROVIDER:
				rule = new IsMessageFromProviderRule(additionalInfo);
				break;
			case IS_MESSAGE_FROM_PERSON:
				long personId = 0;
				
				try
				{
					personId = Long.valueOf(additionalInfo.trim()).longValue();
				}
				catch (NumberFormatException exception)
				{
					throw new SmsSelectorRuleSetException(exception.getMessage());
				}
				rule = new IsMessageFromPersonRule(personId);
				break;
			case IS_MESSAGE_CONTAINS:
				rule = new IsMessageContainsRule(additionalInfo);
				break;
			case IS_MESSAGE_OLDER:
				long date = 0;
				
				try
				{
					date = Long.valueOf(additionalInfo.trim()).longValue();
				}
				catch (NumberFormatException exception)
				{
					throw new SmsSelectorRuleSetException(exception.getMessage());
				}
				rule = new IsMessageOlderRule(date);
				break;
		}
		if(rule != null)
			rule.setId(ruleId);
		
		return rule;
	}
	
	public static RuleId getRuleId(int ruleId)
	{
		RuleId[] ruleIds = RuleId.values();
		
		for(int i = 0; i < ruleIds.length; i++)
		{
			if(ruleId == ruleIds[i].getIntValue())
				return ruleIds[i];
		}
		
		return null;
	}
	
	public static RuleId getRuleId(String ruleName)
	{
		RuleId[] ruleIds = RuleId.values();
		
		for(int i = 0; i < ruleIds.length; i++)
		{
			if(ruleName.equals(ruleIds[i].getName()))
				return ruleIds[i];
		}
		
		return null;
	}
	
	public static int getNumberOfRules()
	{
		return RuleId.values().length;
	}
}

class IsMessageFromAddressRule implements SmsSelectorRuleSet.Rule
{
	public IsMessageFromAddressRule(String address)
	{
		mAddress = address;
	}

	@Override
	public boolean isRuleMatch(Object object)
	{
		SmsSelectorSmsMessage message = (SmsSelectorSmsMessage)object;
		String address = message.getAddress();
		
		return address.equals(mAddress);
	}

	@Override
	public Id getId()
	{
		return mId;
	}
	
	@Override
	public void setId(Id id)
	{
		mId = id;
	}
	
	@Override
	public String getAddtitionalInfo()
	{
		return mAddress;
	}
	
	private String mAddress;
	private Id mId;
}

class IsMessageFromProviderRule implements SmsSelectorRuleSet.Rule
{
	public IsMessageFromProviderRule(String provider)
	{	
		mProvider = provider;
	}
	
	@Override
	public boolean isRuleMatch(Object object)
	{
		SmsSelectorSmsMessage message = (SmsSelectorSmsMessage)object;
		String address = message.getAddress();
		
		return address.contains(mProvider);
	}

	@Override
	public Id getId()
	{
		return mId;
	}
	
	@Override
	public void setId(Id id)
	{
		mId = id;
	}
	
	@Override
	public String getAddtitionalInfo()
	{
		return mProvider;
	}
	
	private String mProvider;
	private Id mId;
}

class IsMessageFromPersonRule implements SmsSelectorRuleSet.Rule
{
	public IsMessageFromPersonRule(long personId)
	{
		mPersonId = personId;
	}
	
	@Override
	public boolean isRuleMatch(Object object)
	{
		SmsSelectorSmsMessage message = (SmsSelectorSmsMessage)object;
		long personId = message.getPersonId();
		
		return personId == mPersonId;
	}

	@Override
	public Id getId()
	{
		return mId;
	}
	
	@Override
	public void setId(Id id)
	{
		mId = id;
	}
	
	@Override
	public String getAddtitionalInfo()
	{
		return String.valueOf(mPersonId);
	}
	
	private long mPersonId;
	private Id mId;
}

class IsMessageContainsRule implements SmsSelectorRuleSet.Rule
{
	public IsMessageContainsRule(String contained)
	{
		mContained = contained;
	}
	
	@Override
	public boolean isRuleMatch(Object object)
	{
		SmsSelectorSmsMessage message = (SmsSelectorSmsMessage)object;
		String messageBody = message.getBody();
		
		return messageBody.contains(mContained);
	}

	@Override
	public Id getId()
	{
		return mId;
	}
	
	@Override
	public void setId(Id id)
	{
		mId = id;
	}
	
	@Override
	public String getAddtitionalInfo()
	{
		return mContained;
	}
	
	String mContained;
	private Id mId;
}

class IsMessageOlderRule implements SmsSelectorRuleSet.Rule
{
	public IsMessageOlderRule(long date)
	{
		mDate = date;
	}
	
	@Override
	public boolean isRuleMatch(Object object)
	{
		SmsSelectorSmsMessage message = (SmsSelectorSmsMessage)object;
		long date = message.getDate();
		
		return date < mDate;
	}

	@Override
	public Id getId()
	{
		return mId;
	}
	
	@Override
	public void setId(Id id)
	{
		mId = id;
	}
	
	@Override
	public String getAddtitionalInfo()
	{
		return String.valueOf(mDate);
	}
	
	private long mDate;
	private Id mId;
}
